<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cors</title>
</head>
<body>
  <h1>CORS</h1>
  <script>
    const xhr = new XMLHttpRequest();
    document.cookie = 'name=1234';
    xhr.withCredentials = true;// 允许携带cookie
    xhr.open('GET', 'http://localhost:4000/getData', true);
    xhr.setRequestHeader('name', 'zhengxinglun');
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4) {
        // 200到300之间都是成功, 304走到缓存
        if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
          console.log(xhr.response);
        }
      }
    };
    xhr.send(); 
  </script>
</body>
</html>